package com.lbyl.fragment.pager;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.lbyl.activitiy.R;
import com.lbyl.adapter.topnews.TopNewsListViewAdapter;
import com.lbyl.domin.TopNewsItem.Contentlist;
import com.lbyl.domin.TopNewsItem.JsonRootBean;
import com.lbyl.util.JsonUtil;
import com.lbyl.util.ConNetUtil;
import com.lbyl.util.GetUrlUtil;
import com.lbyl.view.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 头条新闻中每个小滑块的fragment
 *
 * @author libin996@outlook.com
 */

public class TopNewsFragmentPager extends Fragment {
    private static final String TAG = "TopNewsFragmentPager";

    private static final int MTHOD_ADD = 0;
    private static final int MTHOD_REFESH = 1;
    private int MTHOD_CURRENT = 0;

    private PullToRefreshListView listview_pager_topnews;
    private View view_fragmentpager_topnews;

    private FragmentActivity activity;
    private TopNewsListViewAdapter tncAdapter;
    private String title;
    private int pageNum = 0;


    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        return this.title;
    }

    @Nullable
    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activity = getActivity();
        view_fragmentpager_topnews = inflater.inflate(R.layout.fragmentpager_topnews, container, false);
        initView();
        initData();

        return view_fragmentpager_topnews;
    }


    /**
     *
     */
    private void initData() {

        tncAdapter = new TopNewsListViewAdapter(activity, null);


        listview_pager_topnews.setAdapter(tncAdapter);

        listview_pager_topnews.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
            @Override
            public void onRefreshStart() {
                MTHOD_CURRENT = MTHOD_REFESH;
                pageNum++;
                new TopNewsDataTask().execute(GetUrlUtil.getUrlStrBytitle(title, pageNum));
            }

            @Override
            public void onLoadMore() {

                MTHOD_CURRENT = MTHOD_ADD;
                pageNum++;
                new TopNewsDataTask().execute(GetUrlUtil.getUrlStrBytitle(title, pageNum));
            }

//            @Override
//            public void onRefeshComplete(View header, int padding) {
//
//                header.setPadding(0,padding,0,0);
//                // TODO: 2016/10/10/0010 回弹动画
//                header.setVisibility(View.GONE);
//            }
        });

        initServerData();
    }

    /**
     * 加载第一页网络数据
     */
    private void initServerData() {
        MTHOD_CURRENT = MTHOD_ADD;
        new TopNewsDataTask().execute(GetUrlUtil.getUrlStrBytitle(title, 0));
    }


    private void initView() {
        listview_pager_topnews = (PullToRefreshListView) view_fragmentpager_topnews.findViewById(R.id.listview_pager_topnews);

    }

    /**
     * 下载每个pager所有信息的task类
     */
    class TopNewsDataTask extends AsyncTask<String, Integer, String> {


        @Override
        protected String doInBackground(String... params) {
            //在后台获取json数据,传递到结果中
            return ConNetUtil.httpGetAndRes(params[0]);
//            return (JsonRootBean) JsonUtil.fillBeanByUrl(params[0], JsonRootBean.class);
        }

        @Override
        protected void onPostExecute(String response) {
            super.onPostExecute(response);


            if (response == null) {
                Toast.makeText(activity, "服务器错误", Toast.LENGTH_SHORT).show();
                Log.i(TAG, "onPostExecute: 服务器没有返回数据");
                return;
            }

            //在主线程中,将数据移交adapter处理,更新ui
            JsonRootBean bean = (JsonRootBean) JsonUtil.fillBeanByResponse(response, JsonRootBean.class);

            if (bean.getShowapiResBody().getPagebean().getAllpages() <= pageNum + 1) {
                //服务器几乎没有更多数据.归零pagenum
                pageNum = 0;
            }

            ArrayList<Contentlist> contentlist = (ArrayList<Contentlist>) bean.getShowapiResBody().getPagebean().getContentlist();
            //根据状态进行刷新或添加
            if (MTHOD_CURRENT == MTHOD_ADD) {
                addData(contentlist);
            } else if (MTHOD_CURRENT == MTHOD_REFESH) {
                refreshData(contentlist);
            }
        }

        /**
         * 调用adapter的add方法
         *
         * @param data
         */
        private void addData(List<Contentlist> data) {
            tncAdapter.addItems(data);

        }

        private void refreshData(ArrayList<Contentlist> data) {
            tncAdapter.refreshItems(data);

        }

    }

}
